<?php
/**
 * @copyright meila.com
 * @author guanqun@
 * @name 第三方注册接口
 * @param
 *         $nick = 作用
 *
 *
 */
include ('../../__global.php');
include (SERVER_ROOT_PATH . '/include/config/dataRule/ml_datarule_user.php');

class aj_register extends ml_controller {
    private $nick;
    private $gender;
    
    private $arrAlert = array (
            'nick_format' => 'nick_format',
            'nick_len' => 'nick_len',
            'nick_exists' => 'nick_exists' 
    );
    
    function initParam() {
        $this->nick = $this->input ( 'nick','P');
    }
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam() {
        // 检测字符
        if (! preg_match ( ML_DATARULE_USER_NICK_PREG, $this->nick )) {
            $this->api_output ( ML_RCODE_PARAM, '', $this->arrAlert ['nick_format'] );
        }
        // 检测长度
        $len = Tool_string::str_width ( $this->nick );
        if ($len < ML_DATARULE_USER_NICK_MINLEN || $len > ML_DATARULE_USER_NICK_MAXLEN) {
            $this->api_output ( ML_RCODE_PARAM, '', $this->arrAlert ['nick_len'] );
        }
        // 检测禁用关键字
        if (! ml_tool_keyword::haveBasicKillWord ( $this->nick )) {
            $this->api_output ( ML_RCODE_PARAM, $this->arrAlert ['nick_exists'] );
        }
        // 检测敏感词
        if (! Tool_keyword::check_article_kill_keyword ( $this->nick, 'utf8' )) {
            return $this->api_output ( ML_RCODE_PARAM, $this->arrAlert ['nick_exists'] );
        }
    
    }
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main() {
        $session = $this->getSession ();
        $_3rd_id = $session->getVal ( '3rd_id' );
        if (empty ( $_3rd_id ))
            $this->api_output ( ML_RCODE_HACK, '', '异常提交' );
            // 昵称重复性检测
        $isExists = NULL;
        $oAccount = new ml_model_dbUserAccount ();
        if (! $oAccount->isNickExists ( $this->nick, $isExists ))
            $this->api_output ( ML_RCODE_BUSY, '', ML_RMESSAGE_BUSY );
        elseif ($isExists)
            $this->api_output ( ML_RCODE_PARAM, '', $this->arrAlert ['nick_exists'] );
            // 第三方授权信息检测
        $DB_3rd = new ml_model_db3rdService ();
        if (! $DB_3rd->is3rdServiceUser ( $session->getVal ( 'server_id' ), $session->getVal ( '3rd_id' ), $isExists ))
            $this->api_output ( ML_RCODE_BUSY, '', ML_RMESSAGE_BUSY );
        elseif (! empty ( $isExists ))
            $this->api_output ( ML_RCODE_PARAM, '', '当前验证信息已经存在' );
            // 开始注册流程
        if (! $oAccount->createAccountBy3rd ( $this->nick )) {
            $this->api_output ( ML_RCODE_BUSY, '', ML_RMESSAGE_BUSY );
        }
        $uid = $oAccount->insert_id ();
        
        $return = $DB_3rd->create3rdService ( $uid, $session->getVal ( 'server_id' ), $session->getVal ( 'access_token' ), $session->getVal ( 'token_secret' ), $session->getVal ( '3rd_id' ) );
        if (! $return) {
            $this->api_output ( ML_RCODE_BUSY, '', ML_RMESSAGE_BUSY );
        }
        $userinfo = $session->getVal ( 'userinfo' );
        if (! $userinfo ['is_default_headico']) {
            $headuploadurl = OPENAPI_HEAD_UPLOAD . '?imgurl=' . urldecode ( $userinfo ['head_url'] ) . '&uid=' . $uid;
            Tool_http::get ( $headuploadurl );
        }
        $DB_UserExtInfo = new ml_model_dbUserExtInfo ();
        $DB_UserExtInfo->createRecordBy3rd ( $uid, $userinfo ['gender'], $userinfo ['birthday'] );
        
        ml_tool_actionlog::reg_3rd($uid , $session->getVal ( 'server_id' ) , $this->nick , $this->nick , ($userinfo ['gender']=='f' ? ML_DATARULE_USER_GENDER_GIRL : ML_DATARULE_USER_GENDER_BOY));
        // 投放队列
        ml_tool_sendMq::completeRegister ( $uid );
        ml_tool_sendMq::search_user('A', $uid);
        // 投昵称队列
        $arr = array (
                'uid' => $uid,
                'nick' => $this->nick 
        );
        ml_tool_sendMq::update_nick ( $arr );

        $follow_data=array(
            'uid'=>$uid,
            '3rd_id'=>$session->getVal ( '3rd_id' ),
            'server_id'=>$session->$session->getVal ( 'server_id' ),
            'access_token'=>$session->getVal ( 'access_token' ),
        );
        ml_tool_sendMq::add_follow_meila($follow_data);

        $arrAccount ['uid'] = $uid;
        $arrAccount ['nickname'] = $this->nick;
        $arrAccount ['verify_email'] = ML_NOEMAIL;
        $arrAccount ['status'] = ML_USERSTATUS_THIRD;
        $this->loginProxy ( ml_login, $arrAccount );
        
        $this->api_output ( ML_RCODE_SUCC );
    }
}

new aj_register ();
